package com.huifaapp.mall.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.AfterStatusBean;
import com.huifaapp.mall.bean.CompanyBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

public class AfterlogisticsActivity extends AppCompatActivity {

    private TextView shop_name,shop_xj,shop_yj,shop_num,shop_hjzj,hl_companey;
    private ImageView shop_img;
    AfterStatusBean.DataBean dataBean;
    AfterStatusBean.DataBean.GoodsDetailVoBean goodsDetailVoBean;
    AfterStatusBean afterStatusBean;
    private Button bt_commit;
    String afterid;
    EditText dd_num,receive_phone;
    LinearLayout ll_wlgs;
    CompanyBean companyBean;
    List<CompanyBean.DataBean> comdata = new ArrayList<>();
    List<CompanyBean.DataBean> comdid = new ArrayList<>();
    String expressid;

    int wlstate = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_afterlogistics);
        initView();
    }

    private void initView() {
        shop_name = (TextView) findViewById(R.id.logistics_goodsname);
        shop_xj = (TextView) findViewById(R.id.logistics_xj);
        shop_yj = (TextView) findViewById(R.id.logistics_yj);
        shop_num = (TextView) findViewById(R.id.logistics_count);
        shop_img = (ImageView) findViewById(R.id.logistics_shopImage);
        shop_hjzj = (TextView) findViewById(R.id.logistics_gjhj);
        bt_commit = (Button) findViewById(R.id.logistics_btsubmit);
        dd_num = (EditText) findViewById(R.id.logistics_recedh);
        receive_phone = (EditText) findViewById(R.id.logistics_phonenum);
        ll_wlgs = (LinearLayout) findViewById(R.id.logistics_llwlgs);
        hl_companey = (TextView) findViewById(R.id.logistics_hlgs);


        Intent intent = getIntent();
        afterid = intent.getStringExtra("aftersaleId");

        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/getapplyDetail")
                .addParams("aftersaleId",afterid)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {

                        try{
                            Gson gson = new Gson();
                            afterStatusBean = gson.fromJson(response,AfterStatusBean.class);
                            dataBean = afterStatusBean.getData();
                            goodsDetailVoBean = dataBean.getGoodsDetailVo();
                            shop_name.setText(goodsDetailVoBean.getGoodsName());
                            shop_xj.setText("¥  "+goodsDetailVoBean.getPrice()+"");
                            shop_yj.setText("¥  "+goodsDetailVoBean.getOrginPrice()+"");
                            shop_num.setText("X"+goodsDetailVoBean.getCount()+"");
                            Glide.with(AfterlogisticsActivity.this).load(goodsDetailVoBean.getGoodsImg()).into(shop_img);
                            shop_hjzj.setText("共计"+goodsDetailVoBean.getCount()+"件商品   合计: ¥"+dataBean.getRefundAmount()+"(含运费¥:20)");

                        }catch (Exception e){
                            e.printStackTrace();
                        }


                    }
                });
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"project/express/list")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        companyBean = gson.fromJson(response,CompanyBean.class);

                        comdata.clear();
                        comdata.addAll(companyBean.getData());
                        comdid.clear();
                        comdid.addAll(companyBean.getData());



                    }
                });
        inData();


        final String expressNo = dd_num.getText().toString();
        final String phone = receive_phone.getText().toString();
        final String exname = hl_companey.getText().toString();
        bt_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OkHttpUtils.post().url(ApiManager.BASE_NIUPAI_URL+"orders/afterSaleExpress")
                        .addParams("appId", AUniqueIdentifier.getDeviceId(AfterlogisticsActivity.this))
                        .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                        .addParams("afterSafeId",afterid)
                        .addParams("expressId",expressid)
                        .addParams("expressNo",expressNo)
                        .addParams("phone",phone)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {

                            }

                            @Override
                            public void onResponse(String response) {
                                try {
                                    AlertDialog alertDialog = new AlertDialog.Builder(AfterlogisticsActivity.this).setTitle("提示").setMessage("提交成功")
                                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {

                                                }
                                            }).create();
                                    alertDialog.show();

                                }catch (Exception e){
                                    e.printStackTrace();
                                }


                            }
                        });
            }
        });
    }

    private void inData() {
        ll_wlgs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show(wlstate, comdata, "货流公司", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        wlstate = (int) view.getTag();
                        hl_companey.setText(comdata.get(wlstate).getName());
                        expressid = comdid.get(wlstate).getId();
                    }
                });

            }
        });

    }


    private void show(int selectIndex, List<CompanyBean.DataBean> arr, String title, final View.OnClickListener onClickListener) {
        final Dialog dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
        //该数值表示需要显示的数据源
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_sqyy, null);
        LinearLayout listLayout = (LinearLayout) inflate.findViewById(R.id.listLayout);
        TextView menuTitle = (TextView) inflate.findViewById(R.id.menuTitle);
        menuTitle.setText(title);

        View closeBtn = inflate.findViewById(R.id.closeBtn);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        for (int i = 0; i < arr.size(); i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.item_dialog_list_layout, null);
            TextView textTitle = (TextView) view.findViewById(R.id.textTitle);
            final ImageView selectImage = (ImageView) view.findViewById(R.id.selectImage);
            textTitle.setText(arr.get(i).getName());
            if (selectIndex == i) {
                selectImage.setImageResource(R.drawable.porfile_icon_set_sel);
            } else {
                selectImage.setImageResource(R.drawable.porfile_icon_set_nor);
            }
            view.setTag(i);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onClickListener != null) {
                        onClickListener.onClick(view);
                    }
                    selectImage.setImageResource(R.drawable.porfile_icon_set_sel);
                    dialog.dismiss();
                }
            });

            listLayout.addView(view);
        }
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity( Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 20;
        //设置Dialog距离底部的距离
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;

//       将属性设置给窗体
        dialogWindow.setAttributes(lp);
        dialog.show();//显示对话框
        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

    }


}
